package com.yufeng.collaboration.controller;


import com.yufeng.collaboration.entity.Document;
import com.yufeng.collaboration.service.DocumentService;
import com.yufeng.collaboration.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/documents")
public class DocumentController {

    @Autowired
    private DocumentService documentService;

    @Autowired
    private MessageService messageService;

    @PostMapping("/update")
    public ResponseEntity<String> updateDocument(@RequestBody Document document) {
        try {
            documentService.updateDocument(document);
            messageService.sendDocumentUpdateMessage(document);
            return new ResponseEntity<>("Document updated successfully", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>("Error updating document", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<Document>> getDocumentsByUserId(@PathVariable Long userId) {
        try {
            List<Document> documents = documentService.getDocumentsByUserId(userId);
            return new ResponseEntity<>(documents, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}    